package com.corpgovernment.organization.dataloader;

import com.corpgovernment.api.supplier.bo.suppliercompany.*;
import com.corpgovernment.api.supplier.soa.SupplierCompanyClient;
import com.corpgovernment.api.supplier.soa.constant.ProductTypeEnum;
import com.corpgovernment.api.supplier.vo.MbSupplierInfoVo;
import com.corpgovernment.api.supplier.vo.request.LoadSupplierInfoReq;
import com.corpgovernment.common.base.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author jhxue
 */
@Component
@Slf4j
public class SupplierCompanyClientLoader {

	@Autowired
	private SupplierCompanyClient supplierCompanyClient;

	/**
	 * 查询供应商信息
	 *
	 * @param request
	 * @return
	 */
	public List<SupplierProductBo> listSupplierProduct(ListSupplierProductRequestBo request) {
		if (request == null) {
			return new ArrayList<>();
		}
		JSONResult<List<SupplierProductBo>> result = supplierCompanyClient.listSupplierProduct(request);
		if (result == null || result.getData() == null) {
			log.error("查询供应商信息异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
			return new ArrayList<>();
		}
		return result.getData();
	}


	/**
	 * 查询企业订购服务信息
	 *
	 * @param request
	 * @return
	 */
	public SupplierCompanyBo findSupplierCompany(GetSupplierCompanyRequestBo request) {
		if (request == null) {
			return null;
		}
		request.setProductType(ProductTypeEnum.flight.name());
		JSONResult<SupplierCompanyBo> result = supplierCompanyClient.findSupplierCompany(request);
		if (result == null || result.getData() == null) {
			log.error("查询企业订购服务信息异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
			return null;
		}
		return result.getData();
	}

	/**
	 * 查询供应商信息
	 *
	 * @param request
	 * @return
	 */
	public List<MbSupplierInfoVo> loadSupplierInfo(LoadSupplierInfoReq request) {
		if (request == null) {
			return new ArrayList<>();
		}
		JSONResult<List<MbSupplierInfoVo>> result = supplierCompanyClient.loadSupplierInfo(request);
		if (result == null || result.getData() == null) {
			log.error("查询供应商信息异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
			return new ArrayList<>();
		}
		return result.getData();
	}

	/**
	 * 批量查询供应商
	 *
	 * @param request
	 * @return
	 */
	public List<SupplierCompanyBo> listSupplierCompany(ListSupplierCompanyRequestBo request) {
		if (request == null) {
			return new ArrayList<>();
		}
		request.setProductType(ProductTypeEnum.flight.name());
		JSONResult<List<SupplierCompanyBo>> result = supplierCompanyClient.listSupplierCompany(request);
		if (result == null || result.getData() == null) {
			return null;
		}
		return result.getData();
	}


	/**
	 * 查询产线支持的支付类型
	 *
	 * @param request
	 * @return
	 */
	public List<String> listPubOwnType(ListPubOwnRequestBo request) {
		if (request == null) {
			return new ArrayList<>();
		}
		request.setProductType(ProductTypeEnum.flight.name());
		JSONResult<List<String>> result = supplierCompanyClient.listPubOwnType(request);
		if (result == null || result.getData() == null) {
			log.error("查询产线支持的支付类型异常:" + Optional.ofNullable(result).map(JSONResult::getMsg).orElse("接口无响应"));
			return new ArrayList<>();
		}
		return result.getData();
	}
}
